#include<iostream>
#include<string>
#include<pthread.h>
#include<unistd.h>

using namespace std;

__thread int g_val = 100;

//int g_val = 100;

// std::string hexAddr(pthread_t tid)
// {
//     g_val++;
//     char buffer[64];
//     snprintf(buffer, sizeof(buffer), "0x%x", tid);

//     return buffer;
// }
void *threadRoutine(void* args)
{
    string name = static_cast<const char*>(args);
    int cnt = 5;
    while(cnt)
    {
        //cout << name << " : " << cnt-- << " : " << pthread_self() << " &cnt: " << &cnt << endl;
        cout << name << " g_val: " << g_val++ << ", &g_val: " << &g_val << endl;
        sleep(1);
    }
    return nullptr;
}

int main()
{
    pthread_t t1, t2, t3;
    pthread_create(&t1, nullptr, threadRoutine, (void*)"thread 1"); // 线程被创建的时候，谁先执行不确定！
    pthread_create(&t2, nullptr, threadRoutine, (void*)"thread 2"); // 线程被创建的时候，谁先执行不确定！
    pthread_create(&t3, nullptr, threadRoutine, (void*)"thread 3"); // 线程被创建的时候，谁先执行不确定!

    pthread_join(t1, nullptr);
    pthread_join(t2, nullptr);
    pthread_join(t3, nullptr);

   
    return 0;
}
